package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.SysDataPermission;
import com.yuyou.fn.platform.mapper.AreaTeamMapper;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.ISysDataPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.geom.Area;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * AreaTeam 表数据服务层接口实现类
 *
 */
@Service
public class AreaTeamServiceImpl extends SuperServiceImpl<AreaTeamMapper, AreaTeam,Long> implements IAreaTeamService {

    @Resource
    private ISysDataPermissionService sysDataPermissionService;


    @Override
    @CacheEvict(value = "CacheOfAreaTeam",allEntries = true)
    public void deleteAreaTeam(Long areaTeamId) {
        deleteById(areaTeamId);
    }

    @Override
    public Page<AreaTeam> findAreaTeamForManage(DataPermission dataPermission, int pageNo, int pageSize) {
        Example<AreaTeam> example = AreaTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        if(dataPermission.getUserType() == 0 || dataPermission.getUserType() == 1 ){
            CriteriaUtils.fillConditionForAreaTeamPermission( criteria,dataPermission.getAreaTeamIds() );
        }else{
            criteria.andCondition(" 1 <> 1 ");
        }
        example.orderBy("createTime").desc();
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    @CacheEvict(value = "CacheOfAreaTeam",allEntries = true)
    public void saveOrUpdateAreaTeam(Long areaTeamId, String name , String province , String city ) {

        if( ( areaTeamId == null && name == null ) || (areaTeamId != null && name == null )  ){
            throw new IllegalArgumentException("Illegal arguments");
        }
        name=name.trim();
        if (StringUtils.isEmpty(name)){
            throw new BusException("名称不能为空");
        }
        if(areaTeamId == null ){
            AreaTeam areaTeam = new AreaTeam();
            areaTeam.setName(name);
            areaTeam.setProvince(province);
            areaTeam.setCity(city);
            areaTeam.setCreateTime(TimeUtils.now());
            areaTeam.setUpdateTime(TimeUtils.now());
            checkAreaTeam(areaTeam);
            insertSelective(areaTeam);

            SysDataPermission sysDataPermission = new SysDataPermission();
            sysDataPermission.setUserId(Principals.get().id);
            sysDataPermission.setResourceId(areaTeam.getAreaTeamId());
            sysDataPermission.setResourceType(0);
            sysDataPermission.setCreateTime(TimeUtils.now());
            sysDataPermission.setUpdateTime(TimeUtils.now());
            sysDataPermissionService.insertSelective(sysDataPermission);
            sysDataPermissionService.clearCache();

        }else{
            AreaTeam areaTeam = new AreaTeam();
            areaTeam.setAreaTeamId(areaTeamId);
            areaTeam.setProvince(province);
            areaTeam.setCity(city);
            areaTeam.setName(name);
            areaTeam.setUpdateTime(TimeUtils.now());
            checkAreaTeam(areaTeam);
            updateSelectiveById(areaTeam);
        }

    }

    private void checkAreaTeam(AreaTeam areaTeam) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("name",areaTeam.getName());
        AreaTeam one = findOneByExample(example);
        if(one != null && (areaTeam.getAreaTeamId() == null || !areaTeam.getAreaTeamId().equals(one.getAreaTeamId()))){
            throw new BusException("区域名重复");
        }

    }

    @Override
    public List<AreaTeam> findAreaTeamForList(Long  areaTeamId) {

        Example<AreaTeam> example = AreaTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,areaTeamId, null );
        return findListByExample(example);
    }

    @Override
    public String findTeamNameById(Long areaTeamId) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId);
        example.selectProperties("name");
        AreaTeam areaTeam = findOneByExample(example);
        return areaTeam == null ? StringUtils.EMPTY : areaTeam.getName() ;
    }

    @Override
    public AreaTeam findAreaTeamByName(String areaTeamName) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("name",areaTeamName);
        return findOneByExample(example);
    }

    @Override
    public List<AreaTeam> findAreaTeamByNames(List<String> areaTeamNames) {
        if (areaTeamNames==null || areaTeamNames.size()<=0){
            return Collections.EMPTY_LIST;
        }

        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andIn("name",areaTeamNames);
        return findListByExample(example);
    }

    @Override
    public List<AreaTeam> findAllAreaTeam() {
        return findListByExample(AreaTeam.createExample());
    }

    @Override
    @Cacheable(value = "CacheOfAreaTeam")
    public Map<Long, AreaTeam> findAreaTeamMap() {
        List<AreaTeam> areaTeamList = this.findAllAreaTeam();
        return BeanUtils.toMap(areaTeamList,"areaTeamId");
    }
}